<template>
	<view class="content" :style="{background: `url(${imgPath}storage/home/home_bg.png)`}">
		<view class="box" >
			<view class="addbank">
				<view class="item">
					<view class="label">
						我的账号
					</view>
					<view class="input text-right">
						{{checkForm.mobile}}
					</view>
				</view>
				<view class="item">
					<view class="label">
						昵称
					</view>
					<view class="input">
						<u--input placeholder="请输入..." border="none" inputAlign="right" v-model="checkForm.nickname"></u--input>
					</view>
				</view>
				<view class="item">
					<view class="label">
						头像
					</view>
					<view class="input">
						<view class="uploadimg">
							<view class="uploadimg-bg">
								<view class="upload" v-if="checkForm.avatar">
									<u-icon name="close-circle-fill" @click="delImage" color="#000" class="del-btn" size="50rpx" bold></u-icon>
									<u--image :showLoading="true" :src="checkForm.avatar" width="160rpx" height="160rpx"></u--image>
								</view>
								<view class="upload" v-else>
									<view class="unpload-btn" @click="openAuth('CAMERA')">
										<!-- <view>点击上传</view> -->
										<view class="unpload-btn-add">
											<u--image :showLoading="true" src="/static/img/uploadimg_add.png" width="30rpx" height="30rpx"></u--image>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- <view class="item" @click="passwordClick">
					<view class="label">
						修改密码
					</view>
					<u-icon name="arrow-right" size="20"></u-icon>
				</view> -->
				<view class="item" @click="gotoAddress">
					<view class="label">
						我的地址
					</view>
					<u-icon name="arrow-right" size="20"></u-icon>
				</view>
				<view class="submit">
					<view class="btn" @click="submit">
						确认修改
					</view>
					<view class="del_btn" @click="showDel">
						注销账号
					</view>
				</view>
			</view>
			
			
		</view>
		
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
	</view>
</template>

<script>
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	export default {
		data() {
			return {
				imgPath: this.$store.state.user.baseURL,
				permissionID:'',
				form: {
					nickname:'',
					avatar:'',
					member_id:''
				},
				checkForm: {
					nickname:'',
					avatar:'',
					mobile:''
				},
				type: '',
				lampColumns:[[
					{
						id:1,
						name:'光明灯',
						money:980,
						select:false
					},
					{
						id:2,
						name:'莲花灯',
						money:1880,
						select:false
					},
					{
						id:3,
						name:'祈福灯',
						money:9999,
						select:false
					}
				]],
				lampShow: false,
				memberHome:{},
				androidAuthCamera: false
			}
		},
		components: {
			ykAuthpup
		},
		onLoad(options) {
			this.memberHome=uni.getStorageSync('memberHome')
			console.log(this.memberHome)
			this.checkForm.mobile = this.memberHome.mobile
			this.checkForm.nickname = this.memberHome.nick
			this.checkForm.avatar = this.memberHome.avatar
			if(this.checkForm.avatar && !this.checkForm.avatar.includes('http') ){
				this.checkForm.avatar = this.$store.state.user.baseURL + this.checkForm.avatar
			}
		},
		methods: {
			showDel(){
				if(this.memberHome.amount>0){
					uni.showModal({
						title:"温馨提示",
						content:'您当前账号还有剩余积分，不可注销，提现后才能操作',
						showCancel:false,
						confirmText:"好的"
					})
				}else{
					uni.showModal({
						title:"温馨提示",
						content:'是否确认注销账户',
						complete: (res) => {
							if(res.confirm){
								this.delAccount()
							}
						}
					})
				}
			},
			delAccount() {
				uni.showModal({
					title:"温馨提示",
					content:"请您再次确认是否注销账号，警惕财产损失",
					complete: (res) => {
						if(res.confirm){
							this.$api.cancellation().then(res => {
								removeToken()
								uni.redirectTo({
									url: '/pages/login/login'
								})
							})
						}
					}
				})
				
			},
			linkurl(type) {
				let url = ''
				if (type == 1) {
					// https://app.fjxmlz.com/htmls/static/lianzhong.apk
					url = "https://app.fjxmlz.com/htmls/static/service.html"
				} else {
					url = "https://app.fjxmlz.com/htmls/static/privacy.html"
				}
				uni.navigateTo({
					url: "/pages/webpage/webpage?link=" + url
				})
			},
			gotoAddress(){
				uni.navigateTo({
					url: "/pages/person/my/deliveryaddress" 
				})
			},
			passwordClick(){
				uni.navigateTo({
					url: '/pages/person/my/modify'
				})
			},
			delImage(){
				this.checkForm.avatar = ''
			},
			androidCameraPermiss(index) {
				plus.android.requestPermissions(['android.permission.CAMERA'], (e) => {
					if (e.deniedAlways.length > 0) {
						this.androidAuthCamera = false
						// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
						uni.showModal({
							title: '相册权限说明',
							content: '请允许打开手机相机相册功能，便于您使用该功能上传您的照片/图片/视频及用户实名认证信息、更新头像时上传图片，请您确认授权，否则无法使用该功能',
							// showCancel: false, // 不显示取消按钮
							success(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass("android.content.Intent");
									var Settings = plus.android.importClass(
										"android.provider.Settings");
									var Uri = plus.android.importClass("android.net.Uri");
									var mainActivity = plus.android.runtimeMainActivity();
									var intent = new Intent();
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts("package", mainActivity.getPackageName(),
										null);
									intent.setData(uri);
									mainActivity.startActivity(intent);
								}
							}
						});
					} else if (e.deniedPresent.length > 0) { //权限被临时拒绝  
						this.androidAuthCamera = false
						// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
						uni.showModal({
							title: '相册权限说明',
							content: '请允许打开手机相机相册功能，便于您使用该功能上传您的照片/图片/视频及用户实名认证信息、更新头像时上传图片，请您确认授权，否则无法使用该功能',
							// showCancel: false, // 不显示取消按钮
							success(res) {
								if (res.confirm) {
									var Intent = plus.android.importClass("android.content.Intent");
									var Settings = plus.android.importClass(
										"android.provider.Settings");
									var Uri = plus.android.importClass("android.net.Uri");
									var mainActivity = plus.android.runtimeMainActivity();
									var intent = new Intent();
									intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
									var uri = Uri.fromParts("package", mainActivity.getPackageName(),
										null);
									intent.setData(uri);
									mainActivity.startActivity(intent);
								}
							}
						});
					} else {
						plus.android.requestPermissions(['android.permission.READ_EXTERNAL_STORAGE'], (e) => {
							if (e.deniedAlways.length > 0) { //权限被永久拒绝  
								// 弹出提示框解释为何需要权限，引导用户打开设置页面开启  
								uni.showModal({
									title: '相册权限说明',
									content: '请允许打开手机相机相册功能，便于您使用该功能拍摄身份证、头像、等信息，请您确认授权，否则无法使用该功能',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass(
												"android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings
												.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity
												.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else if (e.deniedPresent.length > 0) { //权限被临时拒绝  
								this.androidAuthCamera = false
								// 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限  
								uni.showModal({
									title: '相册权限说明',
									content: '请打开相册存储功能,便于您使用该功能拍摄身份证、头像、等信息，请您确认授权，否则无法使用该功能',
									// showCancel: false, // 不显示取消按钮
									success(res) {
										if (res.confirm) {
											var Intent = plus.android.importClass(
												"android.content.Intent");
											var Settings = plus.android.importClass(
												"android.provider.Settings");
											var Uri = plus.android.importClass("android.net.Uri");
											var mainActivity = plus.android.runtimeMainActivity();
											var intent = new Intent();
											intent.setAction(Settings
												.ACTION_APPLICATION_DETAILS_SETTINGS);
											var uri = Uri.fromParts("package", mainActivity
												.getPackageName(),
												null);
											intent.setData(uri);
											mainActivity.startActivity(intent);
										}
									}
								});
							} else {
								this.androidAuthCamera = true
								// 选择照片 uni.chooseImage() 
								this.chooseImage(index)
							}
						})
					}
				})
			},
			//打开自定义权限目的弹框
			openAuth(permissionID){
				this.permissionID = permissionID;//这个是对应的权限 ACCESS_FINE_LOCATION 位置权限 / WRITE_EXTERNAL_STORAGE 存储空间/照片权限 / CAMERA相机权限 / CALL_PHONE 拨打电话
				setTimeout(()=>{
					this.$refs['authpup'].open();
				},500)
			},
			//用户授权权限后的回调
			changeAuth(){
				//这里是权限通过后执行自己的代码逻辑
				uni.chooseImage({
					count: 1,
					success: (e) => {
						console.log(e)
						uni.uploadFile({
							// url: 'https://app.fjxmlz.com/api/attachment/upload',
							url: 'https://zhaobanma.booster-cloud.cn/admin/addImgDeng',
							filePath: e.tempFilePaths[0],
							name: 'file',
							header: {
								'accessToken': this.$store.state.login.token,
							},
							success: (uploadFileRes) => {
								// console.log(uploadFileRes)
								this.checkForm.avatar = JSON.parse(uploadFileRes.data).data.url
							}
						});
						
					}
				})
			},
			chooseImage(type) {
				
			},
			confirmLamp(e){
				let obj= e.value[0]
				obj.select = true
				this.form.lampName = obj.name
				// this.form.price = obj.money
				this.lampShow = false 
			},
			gotoPage(type) {
				if(type==1){
					uni.navigateTo({
						url: '/pages/apply/temapply/temapply'
					})
				}else if(type==2){
					uni.navigateTo({
						url: '/pages/apply/fixapply/fixapply'
					})
				}
				
			},
			submit(){
				this.$api.updateMyselfApi(this.checkForm).then(res => {
					uni.showModal({
						title: '提示',
						content: "操作成功，即将返回",
						complete: (res) => {
							if(res.confirm){
								uni.navigateBack()
							}
						}
					})
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100vh;
		// z-index: -1;
		position: relative;
	}
	.box{
		padding: 40rpx 30rpx;
		z-index: -1;
	}
	.listory{
		display: flex;
		justify-content: end;
		padding-bottom: 30rpx;
		.btn{
			background: #6A2D0A;
			color: #fff;
			font-size: 36rpx;
			font-weight: bold;
			padding: 14rpx 30rpx;
			border-radius: 6rpx;
		}
	}
	
	.addbank{
		z-index: -1;
		background: #FFFFFF;
		border-radius: 10rpx;
		padding: 0 30rpx;
		// min-height: 85vh;
		.item{
			display: flex;
			justify-content: space-between;
			padding: 45rpx 0;
			border-bottom: 1rpx solid #ff5500;
			.input{
				flex: 1;
				padding-left: 20rpx;
			}
			.text-right{
				text-align: right;
			}
			.select{
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 40rpx;
				color: #666666;
				line-height: 44rpx;
				text-align: right;
				font-style: normal;
				padding-left: 20rpx;
				display: flex;
				justify-content: end;
				.select-text{
					padding-right: 22rpx;
				}
			}
		}
	}
	.uploadimg{
		display: flex;
		justify-content: flex-end;
		.uploadimg-bg{
			box-sizing: border-box;
			width: 160rpx;
			height: 160rpx;
			background: url('@/static/img/uploadimg_bg.png');
			background-size: 100% 100%;
			// padding: 32rpx;
			.upload{
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				position: relative;
				.unpload-btn{
					width: 230rpx;
					height: 100%;
					background: linear-gradient( 270deg, #EA6F29 0%, #FAAA7D 100%);
					border-radius: 16rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 34rpx;
					color: #FFFFFF;
					line-height: 64rpx;
					text-align: center;
					font-style: normal;
					display: flex;
					justify-content: center;
					align-items: center;
					.unpload-btn-add{
						// padding-left: 14rpx;
					}
				}
				.del-btn{
					position: absolute;
					z-index: 99;
					right: -20rpx;
					top: -20rpx;
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
	}
	.submit{
		padding: 80rpx 0 ;
		.btn{
			width: 630rpx;
			height: 84rpx;
			background: linear-gradient( 270deg, #EA6F29 0%, #FAAA7D 100%);
			border-radius: 16rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 84rpx;
			text-align: center;
			font-style: normal;
		}
		.del_btn{
			width: 100%;
			height: 84rpx;
			background: none;
			font-size: 28rpx;
			color: #a8a8a8;
			line-height: 84rpx;
			text-align: center;
			margin-top: 20rpx;
		}
	}
	
</style>